import Axios from 'axios';
import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router/index';
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    data: {
      index: null,
      category: null,
      verify_code: null,
      token: null,
      userInfo: null,
      expires_time: null,
      cartproduct: null,
      addressList: null,
      interface: null,
      list: {
        cartRecommend: null,
        indexRecommend: null,
        hotgoods: null,
        collect: [],
      }
    },
  },
  getters: {
    isLoginInfo(state) {
      return state.data.token && state.data.userInfo;
    },
    isCollect(state) {
      return state.data.token && state.data.list.collect.length != 0 && state.data.userInfo;
    }
  },
  mutations: {
    getIndexData(state, payload) {
      state.data.index = payload;
      localStorage.setItem("IndexData", JSON.stringify(payload))
    },
    getCategoryData(state, payload) {
      state.data.category = payload;
    },
    getVerifyCode(state, payload) {
      state.data.verify_code = payload;
    },
    getTokenData(state, payload) {
      state.data.token = payload.token;
      state.data.expires_time = payload.expires_time;
      localStorage.setItem("tokenData", JSON.stringify(payload));
    },
    getAddress(state, payload) {
      state.data.addressList = payload;
    },
    getUserInfo(state, payload) {
      state.data.userInfo = payload;
      if (router.app._route.path != '/') {
        if (state.data.interface == '/login' || state.data.interface == null || state.data.interface == '/user/user_data') {
          router.push('/')
        } else {
          router.push(state.data.interface)
        }
      }
    },
    setTokenData(state) {
      var tokenString = localStorage.getItem("tokenData")
      if (tokenString) {
        var tokenObj = JSON.parse(tokenString);
        var NowTime = new Date().getTime();
        var expiresTime = new Date(tokenObj.expires_time).getTime();
        if (NowTime > expiresTime) return;
        state.data.token = tokenObj.token;
        state.data.expires_time = tokenObj.expires_time;
        this.dispatch("getUser");
      }
    },
    getCartProduct(state, payload) {
      state.data.cartproduct = payload;
    },
  },
  actions: {
    getUser(context) {
      var token = context.state.data.token;
      Axios({
        method: "GET",
        url: "http://47.115.51.185/api/user",
        headers: {
          'Authori-zation': 'Bearer ' + token
        }
      }).then(response => {
        context.commit("getUserInfo", response.data.data)
      }).then(() => {
        context.dispatch('getCollectlist', token)
      })
    },
    getList(context, params) {
      Axios.get(`http://47.115.51.185/api${params.url}?page=1&limit=20`).then(response => {
        context.state.data.list[params.name] = response.data.data
      })
    },
    getCollectlist(context, token) {
      Axios({
        method: "GET",
        url: "http://47.115.51.185/api/collect/user?page=1&limit=20",
        headers: {
          'Authori-zation': 'Bearer ' + token
        }
      }).then(response => {
        context.state.data.list.collect = response.data.data
      })
    }
  },
  modules: {
  }
})
